//
//  GHSegmentBtnView.m
//  HaoLyyPro
//
//  Created by 廖光辉 on 26/07/2017.
//  Copyright © 2017 jk. All rights reserved.
//

#import "GHSegmentBtnView.h"
@interface GHSegmentBtnView ()
@property (strong, nonatomic) UILabel *leftLabel;
@property (strong, nonatomic) UILabel *rightLabel;
@property (strong, nonatomic) UIView *redView;
@end

@implementation GHSegmentBtnView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configView];
        [self configTapGestureRecognizer];
    }
    return self;
}

- (void)configView {
    self.leftLabel = [[UILabel alloc] init];
    self.leftLabel.backgroundColor = [UIColor whiteColor];
    self.leftLabel.userInteractionEnabled = YES;
    self.leftLabel.textAlignment = NSTextAlignmentCenter;
    self.leftLabel.font = UIFontPFSCForSize(14);
    self.leftLabel.textColor = [UIColor darkGrayColor];
    [self addSubview:self.leftLabel];
    
    self.rightLabel = [[UILabel alloc] init];
    self.rightLabel.backgroundColor = [UIColor whiteColor];
    self.rightLabel.userInteractionEnabled = YES;
    self.rightLabel.textAlignment = NSTextAlignmentCenter;
    self.rightLabel.font = UIFontPFSCForSize(14);
    self.rightLabel.textColor = [UIColor darkGrayColor];
    [self addSubview:self.rightLabel];
    
    self.redView = [[UIView alloc] init];
    self.redView.backgroundColor = RGB(34, 132, 223);
    [self addSubview:self.redView];
}

- (void)configTapGestureRecognizer {
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1Click)];
    [self.leftLabel  addGestureRecognizer:tap1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2Click)];
    [self.rightLabel  addGestureRecognizer:tap2];
}

- (void)tap1Click {
    [UIView animateWithDuration:0.25 animations:^{
        self.redView.left = (kScreenWidth * 0.25) - 12.5;
        self.leftLabel.textColor = RGB(34, 132, 223);
        self.rightLabel.textColor = [UIColor darkGrayColor];
    }];
    
    if (self.delegate &&  [self.delegate respondsToSelector:@selector(gh_bankStatusLeftLabelClickDelegate)]) {
        [self.delegate gh_bankStatusLeftLabelClickDelegate];
    }
}

- (void)tap2Click {
    [UIView animateWithDuration:0.25 animations:^{
        self.redView.left = self.width * 0.5 + (kScreenWidth * 0.25) - 12.5;
        self.rightLabel.textColor = RGB(34, 132, 223);
        self.leftLabel.textColor = [UIColor darkGrayColor];
    }];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(gh_bankStatusRightLabelClickDelegate)]) {
        [self.delegate gh_bankStatusRightLabelClickDelegate];
    }
}

- (void)setLeftTitle:(NSString *)leftTitle {
    _leftTitle = leftTitle;
    self.leftLabel.textColor = RGB(34, 132, 223);
    self.rightLabel.textColor = [UIColor darkGrayColor];
    self.leftLabel.text = leftTitle;
}

- (void)setRightTitle:(NSString *)rightTitle {
    _rightTitle = rightTitle;
    
    self.rightLabel.text = rightTitle;
}

- (void)changeViewLeft:(CGFloat)left {
    self.redView.left = left * 0.5 + (kScreenWidth * 0.25) - 12.5;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.leftLabel.frame = CGRectMake(0, 0, kScreenWidth * 0.5 - 0.5, 43);
    self.redView.frame = CGRectMake((kScreenWidth * 0.25) - 12.5, 41, 25, 3);
    self.rightLabel.frame = CGRectMake(kScreenWidth * 0.5, 0, kScreenWidth * 0.5 - 0.5, 43);
    UIView *bottomShadowView = [[UIView alloc]initWithFrame:CGRectMake(0, 43.5, kScreenWidth, 0.5)];
    bottomShadowView.backgroundColor = RGB(250, 250, 250);
    [self addSubview:bottomShadowView];
}

- (void)setIsScrollToEnd:(BOOL)isScrollToEnd{
    _isScrollToEnd = isScrollToEnd;
    if (isScrollToEnd) {
        self.rightLabel.textColor = RGB(34, 132, 223);
        self.leftLabel.textColor = [UIColor darkGrayColor];
    }else{
        self.leftLabel.textColor = RGB(34, 132, 223);
        self.rightLabel.textColor = [UIColor darkGrayColor];
    }
}


@end
